package com.gdut.simple.model.impl;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.gdut.simple.basic.BasicApplication;
import com.gdut.simple.common.LogUtils;
import com.gdut.simple.common.RequestCallback;
import com.gdut.simple.common.ToastUtils;
import com.gdut.simple.data.entity.User;
import com.gdut.simple.model.IFollowModel;
import com.gdut.simple.view.activity.explore.OtherUserInfoActivity;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.datatype.BmobPointer;
import cn.bmob.v3.datatype.BmobRelation;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.UpdateListener;

public class FollowModelImpl implements IFollowModel {

    @Override
    public void getUser(String userId, final RequestCallback<User> callback) {
        BmobQuery<User> query = new BmobQuery<>();
        query.addWhereEqualTo("objectId", userId);
        query.setLimit(1);
        query.findObjects(new FindListener<User>() {
            @Override
            public void done(List<User> list, BmobException e) {
                if (e != null) {
                    if (callback != null) {
                        callback.onFailure(e.getMessage());
                    }
                    return;
                }
                if (list != null && list.size() > 0) {
                    if (callback != null) {
                        callback.onSuccess(list.get(0));
                    }
                } else {
                    callback.onFailure("null");
                }
            }
        });
    }

    @Override
    public void checkStatus(User user, final User follow, final RequestCallback<Boolean> callback) {
        if (user == null || follow == null) {
            return;
        }
        if (user.getObjectId().equals(follow.getObjectId())) {
            return;
        }
        BmobQuery<User> query = new BmobQuery<>();
        query.addWhereRelatedTo("follow", new BmobPointer(user));
        query.include("user");
        query.findObjects(new FindListener<User>() {
            @Override
            public void done(List<User> list, BmobException e) {
                if (e != null) {
                    if (callback != null) {
                        callback.onFailure(e.getMessage());
                    }
                    return;
                }
                boolean isFollowed = false;
                for (User res : list) {
                    if (res.getObjectId().equals(follow.getObjectId())) {
                        isFollowed = true;
                        break;
                    }
                }
                if (callback != null) {
                    callback.onSuccess(isFollowed);
                }
            }
        });
    }

    @Override
    public void follow(User user, User follow, final RequestCallback<Boolean> callback) {
        BmobRelation relation = new BmobRelation();
        relation.add(follow);
        user.setFollow(relation);
        user.update(new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if (e == null) {
                    if (callback != null) {
                        callback.onSuccess(true);
                    }
                } else {
                    if (callback != null) {
                        callback.onFailure(e.getMessage());
                    }
                }
            }
        });
    }
}
